package Admin;

import JDBCutil.JDBCUtil;
import bean.Course;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.SQLException;

import static JDBCutil.JDBCUtil.getCourse;
/*
 * Created by JFormDesigner on Sat Aug 10 23:09:33 CST 2024
 */


/**
 * @author 29454
 */
public class ModifyCourse extends JDialog {
    public ModifyCourse(Window owner) throws Exception {
            super(owner);
            initComponents();
            try{
            showinfo();
            setVisible(true);
        }
        catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
//    public static int[] getSelectedid(JTable table){
//        int[] selected = table.getSelectedRows();
//        int[] ids = new int[selected.length];
//        for (int i = 0; i < selected.length; i++) {
//            int rowindex=selected[i];
//            Object idobj=table.getValueAt(rowindex,0);
//            ids[i] = Integer.parseInt(idobj.toString());
//
//        }
//        return ids;
//    }
    public void showinfo() throws Exception {
        int[] ids = Admin.getSelectedid(ShowCourse.getTable1());
        Course c = new Course();
        c = getCourse(ids[0]);
        textField1.setText(c.getCourseid().toString());
        textField1.setEnabled(false);
        textField2.setText(c.getCoursename());
        textField3.setText(c.getTeacherid().toString());
        textField4.setText(c.getCoursetime());
        textField5.setText(c.getClassroom());
        textField6.setText(c.getCourseweek().toString());
    }

    private void modifystu(ActionEvent e) {
        // TODO add your code here
        try {
            int[] ids = Admin.getSelectedid(ShowCourse.getTable1());
            Course c = getCourse(ids[0]);
            c = getCourse(ids[0]);
            c.setCoursename(textField2.getText());
            c.setTeacherid(Integer.valueOf(textField3.getText()));
            c.setCoursetime(textField4.getText());
            c.setClassroom(textField5.getText());
            c.setCourseweek(Integer.valueOf(textField6.getText()));
            JDBCUtil.modifyCourse(c);
            dispose();
        }
        catch (SQLException e1) {
            e1.printStackTrace();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();

        textField1 = new JTextField();
        textField2 = new JTextField();
        textField3 = new JTextField();
        textField4 = new JTextField();
        textField5 = new JTextField();
        textField6 = new JTextField();
        button1 = new JButton();

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label3 ----
        label3.setText("课程id");
        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 5f));
        contentPane.add(label3);
        label3.setBounds(75, 70, 80, 30);

        //---- label4 ----
        label4.setText("课程名");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 5f));
        contentPane.add(label4);
        label4.setBounds(75, 120, 80, 30);

        //---- label5 ----
        label5.setText("任课教师");
        label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 5f));
        contentPane.add(label5);
        label5.setBounds(75, 170, 80, 30);

        //---- label6 ----
        label6.setText("上课时间");
        label6.setFont(label6.getFont().deriveFont(label6.getFont().getSize() + 5f));
        contentPane.add(label6);
        label6.setBounds(75, 220, 80, 30);

        //---- label7 ----
        label7.setText("上课地点");
        label7.setFont(label7.getFont().deriveFont(label7.getFont().getSize() + 5f));
        contentPane.add(label7);
        label7.setBounds(75, 270, 80, 30);

        //---- label8 ----
        label8.setText("学时");
        label8.setFont(label8.getFont().deriveFont(label8.getFont().getSize() + 5f));
        contentPane.add(label8);
        label8.setBounds(75, 320, 80, 30);


        contentPane.add(textField1);
        textField1.setBounds(170, 80, 150, textField1.getPreferredSize().height);
        contentPane.add(textField2);
        textField2.setBounds(170, 130, 150, textField2.getPreferredSize().height);
        contentPane.add(textField3);
        textField3.setBounds(170, 180, 150, textField3.getPreferredSize().height);
        contentPane.add(textField4);
        textField4.setBounds(170, 230, 150, textField4.getPreferredSize().height);
        contentPane.add(textField5);
        textField5.setBounds(170, 280, 150, textField5.getPreferredSize().height);
        contentPane.add(textField6);
        textField6.setBounds(170, 330, 150, textField6.getPreferredSize().height);



        //---- button1 ----
        button1.setText("\u4fee\u6539");
        button1.setFont(button1.getFont().deriveFont(button1.getFont().getSize() + 5f));
        button1.addActionListener(e -> modifystu(e));
        contentPane.add(button1);
        button1.setBounds(160, 425, 85, 45);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JLabel label9;
    private JLabel label10;
    private JTextField textField1;
    private JTextField textField2;
    private JTextField textField3;
    private JTextField textField4;
    private JTextField textField5;
    private JTextField textField6;
    private JTextField textField7;
    private JTextField textField8;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
}
